import React, { useState } from 'react'
import useBounce from '../../hooks/useBounce'
import styles from './index.module.scss'
import http from '../../utils/http'
import { useNavigate } from 'react-router-dom'
import { EyeInvisibleOutline, EyeOutline } from 'antd-mobile-icons'
import {
  Form,
  Input,
  Button,
} from 'antd-mobile'
function Index() {
  const navigate = useNavigate()
  const [visible, setVisible] = useState(false)
  const onFinish = useBounce(async (values) => {
    const res = await http.post('http://81.70.224.2:8003/login', values)

    console.log(res);

    const { code, data } = res.data

    if (code === 200) {
      localStorage.setItem('token', data.token)
      localStorage.setItem('userInfo', JSON.stringify(data.userInfo))
      navigate('/')
    }
  }, 800)
  return (
    <div>
      <Form
        onFinish={onFinish}
        layout='horizontal'
        footer={
          <Button block type='submit' color='primary' size='large'>
            登录
          </Button>
        }
      >
        <Form.Header>水平布局表单</Form.Header>
        <Form.Item
          name='username'
          label='手机号'
          rules={[
            { required: true, message: '手机号不能为空' },
            { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确' }
          ]}
        >
          <Input onChange={console.log} placeholder='请输入手机号' />
        </Form.Item>
        <Form.Item
          name='password'
          label='密码'
          rules={[
            { required: true, message: '密码不能为空' },
            { pattern: /^\w{6,12}$/, message: '密码格式不正确' }
          ]}
        >
          <div className={styles.password}>
            <Input
              className={styles.input}
              placeholder='请输入密码'
              type={visible ? 'text' : 'password'}
            />
            <div className={styles.eye}>
              {!visible ? (
                <EyeInvisibleOutline onClick={() => setVisible(true)} />
              ) : (
                <EyeOutline onClick={() => setVisible(false)} />
              )}
            </div>
          </div>
        </Form.Item>
      </Form>
    </div>
  )
}

export default Index
